// Decompiled by Jad v1.5.8e2. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://kpdus.tripod.com/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi space 
// Source File Name:   PageImpl.java
package com.ylkj.common.page;

import java.io.Serializable;
import java.util.*;

public class PageImpl
	implements Page, Serializable
{

	private static final long serialVersionUID = 0xd803ca7407af78fL;
	private final List content = new ArrayList();
	private final Pageable pageable;
	private final long total;

	@SuppressWarnings("unchecked")
	public PageImpl(List content, Pageable pageable, long total)
	{
		if (null == content)
		{
			throw new IllegalArgumentException("Content must not be null!");
		} else
		{
			this.content.addAll(content);
			this.total = total;
			this.pageable = pageable;
			return;
		}
	}

	public int getNumber()
	{
		int number;
		if (pageable == null)
			number = 0;
		else
			number = pageable.getPageNumber();
		return number;
	}

	public int getSize()
	{
		int size;
		if (pageable == null)
			size = 0;
		else
			size = pageable.getPageSize();
		return size;
	}

	public int getTotalPages()
	{
		int totalPages;
		if (getSize() == 0)
			totalPages = 1;
		else
			totalPages = (int)Math.ceil((double)total / (double)getSize());
		return totalPages;
	}

	public int getNumberOfElements()
	{
		return content.size();
	}

	public long getTotalElements()
	{
		return total;
	}

	public boolean hasPreviousPage()
	{
		return getNumber() > 0;
	}

	public boolean isFirstPage()
	{
		return !hasPreviousPage();
	}

	public boolean hasNextPage()
	{
		return getNumber() + 1 < getTotalPages();
	}

	public boolean isLastPage()
	{
		return !hasNextPage();
	}

	public Pageable nextPageable()
	{
		Pageable next;
		if (hasNextPage())
			next = pageable.next();
		else
			next = null;
		return next;
	}

	public Pageable previousPageable()
	{
		if (hasPreviousPage())
			return pageable.previousOrFirst();
		else
			return null;
	}

	public Pageable getPageable()
	{
		return pageable;
	}

	@SuppressWarnings("unchecked")
	public Iterator<Object> iterator()
	{
		return content.iterator();
	}

	@SuppressWarnings("unchecked")
	public List<Object> getContent()
	{
		return Collections.unmodifiableList(content);
	}

	public boolean hasContent()
	{
		return !content.isEmpty();
	}

	public String toString()
	{
		String contentType = "UNKNOWN";
		if (content.size() > 0)
			contentType = ((Serializable)content.get(0)).getClass().getName();
		return String.format("Page %s of %d containing %s instances", new Object[] {
			Integer.valueOf(getNumber()), Integer.valueOf(getTotalPages()), contentType
		});
	}

	public boolean equals(Object obj)
	{
		if (this == obj)
			return true;
		if (getClass() != obj.getClass())
		{
			return false;
		} else
		{
			PageImpl that = (PageImpl)obj;
			boolean totalEqual = total == that.total;
			boolean contentEqual = content.equals(that.content);
			boolean pageableEqual = pageable != null ? pageable.equals(that.pageable) : that.pageable == null;
			return totalEqual && contentEqual && pageableEqual;
		}
	}

	public int hashCode()
	{
		int result = 17;
		result = 31 * result + (int)(total ^ total >>> 32);
		result = 31 * result + (pageable != null ? pageable.hashCode() : 0);
		result = 31 * result + content.hashCode();
		return result;
	}
}
